# -*- coding: utf-8 -*-

# FILE: OSMRouteBroker.py
# DESC: 
#

from web.service.HttpReader import HttpReader, HttpDocumentNotFound, HttpReadError
from geo.GeoRouteFactory import GeoRouteFactory
from infra.logging import logger

#TODO: move to oreus.conf

#OSM_ROUTE_SERVICE_URL="http://data.giub.uni-bonn.de/openrouteservice/index.php?start=%s,%s&end=%s,%s&lon=%s&lat=%s&zoom=15&pref=Pedestrian&lang=en"

OSM_ROUTE_SERVICE_URL="http://data.giub.uni-bonn.de/openrouteservice/index.php?start=%s,%s&end=%s,%s&lon=%s&lat=%s&zoom=15&pref=Pedestrian&lang=en"

class OSMRouteBroker:

	def findRoutes(self, fromLocation, toLocation):
		log = logger.getLogger()
		
		try:
			httpReader = HttpReader()
			httpReader.addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; fr; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11')
			httpReader.addHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
			httpReader.addHeader('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
			httpReader.addHeader('Content-type', 'application/x-www-form-urlencoded')
						
			
			response = httpReader.read("http://data.giub.uni-bonn.de/openrouteservice/php/OpenLSRS_DetermineRoute.php", \
			    {'Start': '%s,%s' % (fromLocation.lng, fromLocation.lat), \
                 'End': '%s,%s' % (toLocation.lng, toLocation.lat), \
				 'Via': '', \
				 'lang': 'en', \
				 'distunit': 'M', \
				 'routepref': 'Pedestrian', \
				 'avoidAreas': '', \
				 'useTMC': '', \
				 'noMotorways': 'false', \
				 'noTollways': 'false', \
				 'instructions': 'true', \
				 '_': ''})
			
			'''
			response = """<?xml version="1.0" encoding="UTF-8"?>
<xls:XLS xmlns:xls="http://www.opengis.net/xls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" version="1.1" xsi:schemaLocation="http://www.opengis.net/xls http://schemas.opengis.net/ols/1.1.0/RouteService.xsd">
  <xls:ResponseHeader xsi:type="xls:ResponseHeaderType"/>
  <xls:Response xsi:type="xls:ResponseType" requestID="123456789" version="1.1" numberOfResponses="1">
    <xls:DetermineRouteResponse xsi:type="xls:DetermineRouteResponseType">
      <xls:RouteSummary>
        <xls:TotalTime>PT19M22S</xls:TotalTime>
        <xls:TotalDistance uom="M" value="1562.4"/>
        <xls:BoundingBox srsName="EPSG:4326">
          <gml:pos>17.0310400 48.1829211</gml:pos>
          <gml:pos>17.0396000 48.1936990</gml:pos>
        </xls:BoundingBox>
      </xls:RouteSummary>
      <xls:RouteGeometry>
        <gml:LineString srsName="EPSG:4326">
          <gml:pos>17.03104 48.19348</gml:pos>
          <gml:pos>17.031155888400747 48.19352168093826</gml:pos>
          <gml:pos>17.0311361 48.1935767</gml:pos>
          <gml:pos>17.0318161 48.193699</gml:pos>
          <gml:pos>17.0320062 48.1932437</gml:pos>
          <gml:pos>17.0319976 48.1929805</gml:pos>
          <gml:pos>17.0323523 48.1920925</gml:pos>
          <gml:pos>17.0332896 48.192259</gml:pos>
          <gml:pos>17.0333646 48.1920294</gml:pos>
          <gml:pos>17.0334252 48.1918549</gml:pos>
          <gml:pos>17.0335431 48.1915525</gml:pos>
          <gml:pos>17.0347098 48.1903583</gml:pos>
          <gml:pos>17.0348404 48.1901522</gml:pos>
          <gml:pos>17.0355594 48.1902544</gml:pos>
          <gml:pos>17.0363271 48.1883045</gml:pos>
          <gml:pos>17.0375378 48.1853137</gml:pos>
          <gml:pos>17.037681 48.1853397</gml:pos>
          <gml:pos>17.0378302 48.1853637</gml:pos>
          <gml:pos>17.0380848 48.1846902</gml:pos>
          <gml:pos>17.0384305 48.1840204</gml:pos>
          <gml:pos>17.0393163 48.1830049</gml:pos>
          <gml:pos>17.03942762594065 48.1829210747884</gml:pos>
          <gml:pos>17.0396 48.18315</gml:pos>
        </gml:LineString>
      </xls:RouteGeometry>
      <xls:RouteInstructionsList xls:lang="en">
        <xls:RouteInstruction duration="PT1M16S" description="Actionno. 1">
          <xls:Instruction>Start (unknown) on OÅ¾voldÃ­kova</xls:Instruction>
          <xls:distance value="16" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.03104 48.19348</gml:pos>
              <gml:pos>17.031155888400747 48.19352168093826</gml:pos>
              <gml:pos>17.0311361 48.1935767</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT34S" description="Actionno. 2">
          <xls:Instruction>Go right on Gallayova</xls:Instruction>
          <xls:distance value="52" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0311361 48.1935767</gml:pos>
              <gml:pos>17.0318161 48.193699</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT2M2S" description="Actionno. 3">
          <xls:Instruction>Go right on Gallayova</xls:Instruction>
          <xls:distance value="184" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0318161 48.193699</gml:pos>
              <gml:pos>17.0320062 48.1932437</gml:pos>
              <gml:pos>17.0319976 48.1929805</gml:pos>
              <gml:pos>17.0323523 48.1920925</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT48S" description="Actionno. 4">
          <xls:Instruction>Go left on Gallayova</xls:Instruction>
          <xls:distance value="72" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0323523 48.1920925</gml:pos>
              <gml:pos>17.0332896 48.192259</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT53S" description="Actionno. 5">
          <xls:Instruction>Go right on FedÃ¡kova</xls:Instruction>
          <xls:distance value="81" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0332896 48.192259</gml:pos>
              <gml:pos>17.0333646 48.1920294</gml:pos>
              <gml:pos>17.0334252 48.1918549</gml:pos>
              <gml:pos>17.0335431 48.1915525</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT2M2S" description="Actionno. 6">
          <xls:Instruction>Go half left on FedÃ¡kova</xls:Instruction>
          <xls:distance value="183" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0335431 48.1915525</gml:pos>
              <gml:pos>17.0347098 48.1903583</gml:pos>
              <gml:pos>17.0348404 48.1901522</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT36S" description="Actionno. 7">
          <xls:Instruction>Go left on RepaÅ¡skÃ©ho</xls:Instruction>
          <xls:distance value="55" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0348404 48.1901522</gml:pos>
              <gml:pos>17.0355594 48.1902544</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT6M19S" description="Actionno. 8">
          <xls:Instruction>Go right on SaratovskÃ¡</xls:Instruction>
          <xls:distance value="569" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0355594 48.1902544</gml:pos>
              <gml:pos>17.0363271 48.1883045</gml:pos>
              <gml:pos>17.0375378 48.1853137</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT14S" description="Actionno. 9">
          <xls:Instruction>Go left</xls:Instruction>
          <xls:distance value="22" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0375378 48.1853137</gml:pos>
              <gml:pos>17.037681 48.1853397</gml:pos>
              <gml:pos>17.0378302 48.1853637</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
        <xls:RouteInstruction duration="PT4M33S" description="Actionno. 10">
          <xls:Instruction>Go right on M. Sch. TrnavskÃ©ho - Arrived at destination!</xls:Instruction>
          <xls:distance value="328" uom="M"/>
          <xls:RouteInstructionGeometry>
            <gml:LineString srsName="EPSG:4326">
              <gml:pos>17.0378302 48.1853637</gml:pos>
              <gml:pos>17.0380848 48.1846902</gml:pos>
              <gml:pos>17.0384305 48.1840204</gml:pos>
              <gml:pos>17.0393163 48.1830049</gml:pos>
              <gml:pos>17.03942762594065 48.1829210747884</gml:pos>
              <gml:pos>17.0396 48.18315</gml:pos>
            </gml:LineString>
          </xls:RouteInstructionGeometry>
        </xls:RouteInstruction>
      </xls:RouteInstructionsList>
    </xls:DetermineRouteResponse>
  </xls:Response>
</xls:XLS>
"""
			'''			
			geoRouteFactory = GeoRouteFactory()
			return geoRouteFactory.createGeoRoutes(response)
		except HttpDocumentNotFound, e:
			log.error("HttpDocumentNotFound exception caught.")
			raise e
		except HttpReadError, e:
			log.error("HttpReadError exception caught, reason: %s" % e)
			raise e
		except Exception, e:
			log.error('Failed to obtain route from {%s} to {%s}, reason = %e' % (fromLocation, toLocation, e))  
			raise e